// pages/loan/loan.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yearsShow: false,
    interestShow: false,
    mode: [{
      name: '按贷款总额',
      value: 0,
      checked: true
    }, {
      name: '按房屋总价',
      value: 1,
      checked: false
    }],
    columns: [],
    interestList: ['4.65%（LPR+0基点）', '5.19%（LPR+54基点）', '5.2%（LPR+55基点）', '5.3%（LPR+65基点）', '5.4%（LPR+75基点）', '5.44%（LPR+79基点）'],
    interest: '4.65%（LPR+0基点）',
    years: '1年',
    monney: 0,
    interestIndex: 0,
    yearsIndex: 0,
    interestText: null,
    houseShow:false,
    houseMonney:0,
    proportion:0,
    active:0
  },
  radioChange(e){
    // console.log(e)
    this.data.houseShow = (e.detail.value==0?false:true)
    this.setData({
      houseShow:this.data.houseShow,
      active:e.detail.value
    })
  },
  houseInput(e){
    let status = e.currentTarget.dataset.status;
    if(status == 0){
      this.setData({
        houseMonney:e.detail.value
      })
    }else{
      if(e.detail.value <= 10){
        this.setData({
          proportion:e.detail.value
        })
      }else{
        this.setData({
          proportion:10
        })
      }
    }
    this.setData({
      monney:this.data.houseMonney*this.data.proportion/10
    })
  },
  moneyInput(e) {
    this.setData({
      monney: e.detail.value
    })
  },
  setInterest(e) {
    this.setData({
      interestText: e.detail.value
    })
  },
  submit(e) {
    let {
      monney,
      interestIndex,
      yearsIndex,
      interestText,
      houseMonney,
      proportion
    } = this.data
    let percentage = [0.0465, 0.0519, 0.052, 0.053, 0.054, 0.0544]
    if (interestText) interestText = Number(interestText.replace("%", ""));
    let interest = (interestText ? interestText : percentage[interestIndex])
    let data = {
      monney:monney,
      year:yearsIndex+1,
      interest:interest
    }
    if(this.data.active == 1) {
      data.houseMonney = houseMonney
      data.proportion = proportion
    }
    wx.navigateTo({
      url: '../loanEnd/loanEnd?data='+JSON.stringify(data),
    })
  },
  yearConfirm(e) {
    // console.log(e.detail)
    this.setData({
      years: e.detail.value,
      yearsShow: false,
      yearsIndex: e.detail.index
    })
  },
  interestConfirm(e) {
    let value = (e.detail.value.search(/%/) != -1 ? e.detail.value : e.detail.value + '%')
    
    this.setData({
      interest: value,
      interestShow: false,
      interestIndex: e.detail.index
    })
  },
  onClose() {
    this.setData({
      yearsShow: false,
      interestShow: false
    })
  },
  showMode(e) {
    let status = e.currentTarget.dataset.status;
    let that = this;
    switch (Number(status)) {
      case 1:
        that.setData({
          yearsShow: true
        })
        break;

      case 2:
        that.setData({
          interestShow: true
        })
        break;
    }
  },
  onChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    Toast(`当前值：${value}, 当前索引：${index}`);
    this.setData({
      years: value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    for (let i = 1; i < 31; i++) {
      that.data.columns.push(i + '年')
    }
    that.setData({
      columns: that.data.columns
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})